export default {
	state: {
		goodsList: [
			{
				checked: false,
				id:1,
				name: 'Apple iPhone12 苹果12 国行全网通 双卡双待 二手5G手机 红色[赠PD20w快充] 128G全网通(可选品牌电池效率100%) 99成新',
				attr: [
					{
						groupName: '颜色',
						attrValue: '红色'
					},
					{
						groupName: '版本',
						attrValue: '8P'
					},
					{
						groupName: '版本',
						attrValue: '8P'
					},
					{
						groupName: '版本',
						attrValue: '8P'
					},
					{
						groupName: '版本',
						attrValue: '8P'
					},
				],
				price: 27,
				
			},
			{
				checked: false,
				id:2,
				name: '英国viewxxEc 咖啡杯叮当商城发发送',
				attr: [
					{
						groupName: '颜色',
						attrValue: '红色'
					},
					{
						groupName: '版本',
						attrValue: '8P'
					},
				],
				price: 27,
				
			},
			{
				checked: false,
				id:3,
				name: '英国viewxxEc 咖啡杯叮当商城',
				attr: [
					{
						groupName: '颜色',
						attrValue: '红色'
					},
					{
						groupName: '版本',
						attrValue: '8P'
					},
				],
				price: 27,
				
			}
		],
		selectedList: []
	},
	getters:{
		checkedAll(state) {
			return state.goodsList.length === state.selectedList.length
		},
		totalCount(state) {
			let data = {
				amount: 0,
				total: 0
			}
			state.goodsList.forEach(item => {
				if(item.checked) {
					data.amount = data.amount + item.price
					data.total = data.total + 1
				}
			})
			return data
		}
	},
	mutations: {
		checkAll(state) {
			state.selectedList = state.goodsList.map(item => {
				item.checked = true
				return item.id
			})
		},
		uncheckAll(state) {
			state.goodsList.forEach(item => item.checked = false)
			state.selectedList = []
		},
		singleCheck(state,id) {
			let goodsIdx = state.goodsList.findIndex(item => item.id == id)
			if(state.goodsList[goodsIdx].checked) {
				state.goodsList[goodsIdx].checked = !state.goodsList[goodsIdx].checked
				let idx = state.goodsList.findIndex(item => item == id)
				state.selectedList.splice(idx,1)
				return
			}
			state.goodsList[goodsIdx].checked = !state.goodsList[goodsIdx].checked
			state.selectedList.push(id)
		}
	},
	actions: {
		checkAllFun({commit,getters}) {
			getters.checkedAll ? commit("uncheckAll") : commit("checkAll")
		}
	}
}